//
//  ZGSHVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/25.
//

#import "ZGSHVC.h"
#import "ZGSHTableViewCell.h"
#import "ZGSHTableViewCell1.h"
#import "BottomPopupView.h"
#import "BRTextPickerView.h"
#import "BRDatePickerView.h"
@interface ZGSHVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, copy) NSArray * dataArr;
/// 审核结果
@property (nonatomic, copy) NSArray * shjgArr;
/// 接管单位
@property (nonatomic, copy) NSArray * jgdwArr;
/// 截止日期
@property (nonatomic, copy) NSArray * timeArr;

@property (nonatomic, strong) NSMutableDictionary * dataDic;
@property (nonatomic,copy) NSArray * deptList;


@end

@implementation ZGSHVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataDic = [NSMutableDictionary dictionary];
    self.dataDic[@"shzt"] = @"1";
    self.navigationItem.title = @"整改审核";
    self.shjgArr = @[@"通过", @"不通过"];
    self.dataArr = @[
        @{
            @"title":@"审核结果",
            @"content":@"通过"
        }.mutableCopy,
        @{
            @"title":@"截止日期",
            @"content":@"请选择"
        }.mutableCopy,
        @{
            @"title":@"审核原因",
            @"content":@""
        }.mutableCopy
    ];
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetDeptRel parameters:@{} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            weakSelf.deptList = data[@"data"];
        }
        [self initUI];
    } failureBlock:^(NSString * _Nonnull description) {
        [self initUI];

    }];
}


- (void)showShjgPickerPopup {
    
    // 创建底部弹出选择器（默认选中第0项）
    BottomPopupView *popup = [BottomPopupView pickerPopupWithTitle:@"请选择"
                                                             items:self.shjgArr
                                                     selectedIndex:0];
    // 显示弹出框
    kWEAK_SELF;
    [popup showInViewController:[WDGlobal getCurrentViewController] animated:YES completion:^(NSInteger selectedIndex, NSString *selectedItem) {
        NSLog(@"%@-%ld",selectedItem,(long)selectedIndex);
        if (selectedIndex >= 0) {
            
            NSMutableDictionary *dic = self.dataArr[0];
            dic[@"content"] = selectedItem;
            self.dataDic[@"shzt"] = [NSString stringWithFormat:@"%ld",selectedIndex+1];
            [weakSelf.tableView reloadData];
        } else {
        }
    }];
}
#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary *dic = self.dataArr[0];
    if([dic[@"content"] isEqualToString:@"通过"]){
        if(indexPath.row == 1) {
            return 0;
        }
    }
    
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == self.dataArr.count -1){
        ZGSHTableViewCell1 *cell = [ZGSHTableViewCell1 nibCell];
        cell.callBack = ^(NSString * _Nonnull str) {
            self.dataDic[@"shsm"] = str;
        };
        return  cell;
    }
    ZGSHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [ZGSHTableViewCell nibCell];
    }
    if (indexPath.row < self.dataArr.count-1) {
        NSDictionary *dic = self.dataArr[indexPath.row];
        [cell cellWithData:dic];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    if(indexPath.row == 0) {
        [self showShjgPickerPopup];
    }else if (indexPath.row == 1) {
        // 1.创建日期选择器
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
        // 2.设置属性
        datePickerView.pickerMode = BRDatePickerModeYMD;
        datePickerView.title = @"选择年月日";
        datePickerView.isAutoSelect = YES;
        kWEAK_SELF
        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            NSLog(@"选择的值：%@", selectValue);
            dic[@"content"] = selectValue;
            weakSelf.dataDic[@"jzsj"] = selectValue;
            [tableView reloadData];

        };
        
        // 3.显示
        [datePickerView show];
    }
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)initUI {
    
    UIButton *submitBtn = [UIButton buttonWithTitle:@"提交" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16] target:self action:@selector(onSubmi)];
    submitBtn.backgroundColor = main_color;
    submitBtn.cornerRadius = 4;
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.right.offset(-16);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-15);
        make.height.offset(45);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.bottom.equalTo(submitBtn.mas_top);
    }];
}
-(void)onSubmi{
    self.dataDic[@"id"] = @[self.ID];
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetSecuritycheckReview parameters:self.dataDic successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            [MBProgressHUD promptMessage:@"提交成功" inView:nil];
            [weakSelf.navigationController popViewControllerAnimated:true];
        }
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
}
@end
